package com.caojin.propertyInjector.annotation;

import com.caojin.propertyInjector.PropertySearcher;
import com.caojin.propertyInjector.SimpleUrlResolver;
import com.caojin.propertyInjector.UrlResolver;
import com.caojin.propertyInjector.locationResolver.ClassPathLocationResolver;
import com.caojin.propertyInjector.locationResolver.FileSyslocationResolver;
import com.caojin.propertyInjector.locationResolver.LocationResolver;
import com.caojin.propertyInjector.propertyResolver.PropertiesResolerFactoy;
import com.caojin.propertyInjector.propertyResolver.PropertiesResolver;

/**
 * 
 * 注释解析基类,扩展该类实现自定义注解的解析
 * @author caojin
 */
public abstract class AnnotationResolver {

	public final static String UNRESOLVE_ANNOTATION = new String();

	protected UrlResolver urlResolver;

	protected String propsResolverCfg;

	/**
	 * 对注解的解析
	 * @param annotation
	 * @return
	 */
	public String getInjectValue(Object annotation) {
		if (InjectPropFromClassPath.class.isInstance(annotation)) {
			InjectPropFromClassPath injectProp = (InjectPropFromClassPath) annotation;
			String key = injectProp.key();
			String basePackage = injectProp.basePackage();
			String fileName = injectProp.fileName();
			boolean recursive = injectProp.recursive();
			boolean useCache = injectProp.useCache();
			LocationResolver locationResolver = new ClassPathLocationResolver(
					basePackage, fileName, recursive);
			//获取配置文件解析器
			PropertiesResolerFactoy propResolverFactory = new PropertiesResolerFactoy(
					propsResolverCfg);
			PropertiesResolver propertiesResolver = propResolverFactory
					.getInstance(injectProp.fileType());
			propertiesResolver.setConfiguration(locationResolver, useCache);
			PropertySearcher propSearcher = new PropertySearcher(
					propertiesResolver);
			return propSearcher.searchProperty(key);
		} else if (InjectPropFromFileSys.class.isInstance(annotation)) {
			InjectPropFromFileSys injectProp = (InjectPropFromFileSys) annotation;
			String key = injectProp.key();
			String folder = injectProp.folder().equals(
					InjectPropFromFileSys.DEFAULT_FOLDER) ? FileSyslocationResolver.DEFAULT_FILE_FOLDER
					: injectProp.folder();
			String fileName = injectProp.fileName();
			boolean recursive = injectProp.recursive();
			boolean useCache = injectProp.useCache();
			LocationResolver locationResolver = new FileSyslocationResolver(
					folder, fileName, recursive);
			//获取配置文件解析器
			PropertiesResolerFactoy propResolverFactory = new PropertiesResolerFactoy(
					propsResolverCfg);
			PropertiesResolver propertiesResolver = propResolverFactory
					.getInstance(injectProp.fileType());
			propertiesResolver.setConfiguration(locationResolver, useCache);
			PropertySearcher propSearcher = new PropertySearcher(
					propertiesResolver);
			return propSearcher.searchProperty(key);
		} else if (InjectProp.class.isInstance(annotation)) {
			InjectProp injectProp = (InjectProp) annotation;
			String key = injectProp.key();
			String url = injectProp.url();
			boolean useCache = injectProp.useCache();
			//使用默认的url解析器
			if (urlResolver == null) {
				urlResolver = new SimpleUrlResolver();
			}
			urlResolver.setUrl(url);
			LocationResolver locationResolver = urlResolver.resolveUrl();
			PropertiesResolerFactoy propResolverFactory = new PropertiesResolerFactoy(
					propsResolverCfg);
			PropertiesResolver propertiesResolver = propResolverFactory
					.getInstance(injectProp.fileType());
			propertiesResolver.setConfiguration(locationResolver, useCache);
			PropertySearcher propSearcher = new PropertySearcher(
					propertiesResolver);
			return propSearcher.searchProperty(key);
		} else {
			return dealWithOtherAnnotation();
		}
	}

	//其他注解的处理
	public abstract String dealWithOtherAnnotation();

	public void setUrlResolver(UrlResolver urlResolver) {
		this.urlResolver = urlResolver;
	}

	public void setPropsResolverCfg(String propsResolverCfg) {
		this.propsResolverCfg = propsResolverCfg;
	}
}
